package android.slc.popup.impl;

import android.app.Dialog;
import android.content.DialogInterface;
import android.slc.popup.SlcPopup2;

import java.lang.ref.WeakReference;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/04/07 下午 5:20
 */
public class DialogOperateImpl extends BaseOperateImpl<Dialog> implements SlcPopup2.DialogOperate {
    private final OnOperateDismissListener mOnOperateDismissListener;
    private final OnOperateCancelListener mOnOperateCancelListener;
    private final OnOperateShowListener mOnOperateShowListener;
    private DialogInterface.OnCancelListener mOnCancelListener;
    private DialogInterface.OnDismissListener mOnDismissListener;
    private DialogInterface.OnShowListener mOnShowListener;
    private Dialog mDialog;

    public DialogOperateImpl() {
        super();
        this.mOnOperateDismissListener = new OnOperateDismissListener(this);
        this.mOnOperateCancelListener = new OnOperateCancelListener();
        this.mOnOperateShowListener = new OnOperateShowListener();
    }

    @Override
    public SlcPopup2.DialogOperate setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
        this.mOnCancelListener = onCancelListener;
        return this;
    }

    @Override
    public SlcPopup2.DialogOperate setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        this.mOnDismissListener = onDismissListener;
        return this;
    }

    @Override
    public SlcPopup2.DialogOperate setOnShowListener(DialogInterface.OnShowListener onShowListener) {
        this.mOnShowListener = onShowListener;
        return this;
    }

    @Override
    public void dismiss() {
        mDialog.dismiss();
    }

    @Override
    public Dialog getDialog() {
        return this.mDialog;
    }

    @Override
    public void show(Dialog dialog) {
        if (SlcPopup2.existOperateByKey(getKey())) {
            return;
        }
        this.mOnOperateDismissListener.setKey(getKey());
        this.mOnOperateDismissListener.setOnDismissListener(this.mOnDismissListener);
        this.mOnOperateCancelListener.setOnCancelListener(this.mOnCancelListener);
        this.mOnOperateShowListener.setOnShowListener(this.mOnShowListener);
        this.mDialog = dialog;
        this.mDialog.setCancelable(isCancelable());
        this.mDialog.setOnDismissListener(this.mOnOperateDismissListener);
        this.mDialog.setOnCancelListener(this.mOnOperateCancelListener);
        this.mDialog.setOnShowListener(this.mOnOperateShowListener);
        this.mDialog.show();
        SlcPopup2.addOperate(getKey(), this);
    }

    private static class OnOperateDismissListener implements DialogInterface.OnDismissListener {
        private DialogOperateImpl baseOperate;
        private WeakReference<DialogInterface.OnDismissListener> onDismissListenerWeakReference;
        private String key;

        private OnOperateDismissListener(DialogOperateImpl baseOperate) {
            this.baseOperate = baseOperate;
        }

        private void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
            this.onDismissListenerWeakReference = new WeakReference<>(onDismissListener);
        }

        public void setKey(String key) {
            this.key = key;
        }

        @Override
        public void onDismiss(DialogInterface dialog) {
            DialogInterface.OnDismissListener onDismissListener = onDismissListenerWeakReference.get();
            baseOperate.mDialog = null;
            baseOperate = null;
            SlcPopup2.removeOperate(this.key);
            if (onDismissListener != null) {
                onDismissListener.onDismiss(dialog);
            }
        }
    }

    private static class OnOperateCancelListener implements DialogInterface.OnCancelListener {
        private WeakReference<DialogInterface.OnCancelListener> onCancelListenerWeakReference;

        private OnOperateCancelListener() {
        }

        private void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
            this.onCancelListenerWeakReference = new WeakReference<>(onCancelListener);
        }

        @Override
        public void onCancel(DialogInterface dialog) {
            DialogInterface.OnCancelListener onCancelListener = onCancelListenerWeakReference.get();
            if (onCancelListener != null) {
                onCancelListener.onCancel(dialog);
            }
        }
    }

    private static class OnOperateShowListener implements DialogInterface.OnShowListener {
        private WeakReference<DialogInterface.OnShowListener> onShowListenerWeakReference;

        private OnOperateShowListener() {
        }

        private void setOnShowListener(DialogInterface.OnShowListener onShowListener) {
            this.onShowListenerWeakReference = new WeakReference<>(onShowListener);
        }

        @Override
        public void onShow(DialogInterface dialog) {
            DialogInterface.OnShowListener onShowListener = onShowListenerWeakReference.get();
            if (onShowListener != null) {
                onShowListener.onShow(dialog);
            }
        }
    }
}
